using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using SypConst.Garbage;
using SypEngine;
using SypGui.CreateForms;

using Pabo.MozBar;

namespace SypGui.OptionForms
{
    public partial class SypPrefForm : SypFormDesign
    {
        public delegate void SypPrefStatusText(string text);


        private void PrefStatusBar(string text)
        {
            if (this.InvokeRequired)
            {
                SypPrefStatusText d = new SypPrefStatusText(PrefStatusBar);
                this.BeginInvoke(d, new object[] { text });
            }
            else
            {
                if (SypPrefFormStatusBar.Items.Count > 0)
                {
                    ToolStripStatusLabel myStatus = (ToolStripStatusLabel)SypPrefFormStatusBar.Items[0];
                    myStatus.Text = text;
                }
            }
        }

        protected override void InitializeRessources()
        {
            Text = GetRessources("PrefTitleWindow");
            MenuItemGeneral.Text = GetRessources("PrefItemGeneral");
        }

        private void InitializeSiteUC(SypCommandDispatcher SypCmdHandler)
        {
            SuspendLayout();
            List<SypSite> mySites = SypCmdHandler.GetAllSites();

            OptionMainPanel myGeneralPanel = new OptionMainPanel(new SypPrefStatusText(PrefStatusBar), OutputMethod);
            Controls.Add(myGeneralPanel);

            foreach (SypSite mySite in mySites)
            {
                siteControlPanel mySiteControlPane = new siteControlPanel(OutputMethod, new SypPrefStatusText(PrefStatusBar), mySite);
                mySiteControlPane.Name = mySite.SiteId.ToString();
                mySiteControlPane.Hide();

                MozItem mySiteMenuItem = new MozItem();
                mySiteMenuItem.ItemStyle = Pabo.MozBar.MozItemStyle.Text;
                mySiteMenuItem.Location = new System.Drawing.Point(2, 2);
                mySiteMenuItem.Name = mySite.SiteId.ToString();
                mySiteMenuItem.Size = new System.Drawing.Size(100, 21);
                mySiteMenuItem.TabIndex = 0;
                mySiteMenuItem.Text = mySite.SiteTitle;
                mySiteMenuItem.Click += new System.EventHandler(SiteMenuItem_Click);

                MenuPane.Items.Add(mySiteMenuItem);
                Controls.Add(mySiteControlPane);
            }
            ResumeLayout();
        }

        public void AddSite(SypSite mySite)
        {
            SuspendLayout();


            MozItem mySiteMenuItem = new MozItem();
            mySiteMenuItem.ItemStyle = Pabo.MozBar.MozItemStyle.Text;
            mySiteMenuItem.Location = new System.Drawing.Point(2, 2);
            mySiteMenuItem.Name = mySite.SiteId.ToString();
            mySiteMenuItem.Size = new System.Drawing.Size(100, 21);
            mySiteMenuItem.TabIndex = 0;
            mySiteMenuItem.Text = mySite.SiteTitle;
            mySiteMenuItem.Click += new System.EventHandler(SiteMenuItem_Click);

            bool checkifexist = false;
            foreach (MozItem myItem in MenuPane.Items)
            {
                if (myItem.Name == mySite.SiteId.ToString())
                    checkifexist = true;
            }
            if (!checkifexist)
            {
                MenuPane.Items.Add(mySiteMenuItem);
                siteControlPanel mySiteControlPane = new siteControlPanel(OutputMethod, new SypPrefStatusText(PrefStatusBar), mySite);
                mySiteControlPane.Name = mySite.SiteId.ToString();
                mySiteControlPane.Hide();
                Controls.Add(mySiteControlPane);
            }
            else
                SypGarbage.free(mySiteMenuItem);
            ResumeLayout();
        }

        public void RemoveSiteUC(int SiteId)
        {
            SuspendLayout();
            Control myControlToRemove = null;
            foreach (Control myControl in Controls)
            {
                if (myControl is siteControlPanel)
                {
                    if (((siteControlPanel)myControl).Name != SiteId.ToString())
                        myControlToRemove = myControl;
                }
            }
            if (myControlToRemove != null)
                Controls.Remove(myControlToRemove);
            MozItem myItemToRemove = null;
            foreach (MozItem myItem in MenuPane.Items)
            {
                if (myItem.Name == SiteId.ToString())
                    myItemToRemove = myItem;
            }
            if (myItemToRemove != null)
                MenuPane.Items.Remove(myItemToRemove);
            ResumeLayout();
        }

        public void RemoveAllUC()
        {
            SuspendLayout();
            List<Control> mySiteToRemove = new List<Control>();
            List<MozItem> myItemToRemove = new List<MozItem>();
            
            foreach (Control myControl in Controls)
            {
                if (myControl is siteControlPanel)
                     mySiteToRemove.Add(myControl);
            }
            foreach (MozItem myItem in MenuPane.Items)
            {
                if (myItem.Name != "MenuItemGeneral")
                    myItemToRemove.Add(myItem);
            }

            foreach (Control myControl in mySiteToRemove)
                Controls.Remove(myControl);

            foreach (MozItem myItem in myItemToRemove)
                MenuPane.Items.Remove(myItem);

            SypGarbage.free(mySiteToRemove);
            SypGarbage.free(myItemToRemove);
            ResumeLayout();
        }


        public SypPrefForm() : base (){}

        public SypPrefForm(SetText OutputMethod, SypCommandDispatcher SypCmdHandler)
            : base (OutputMethod)
        {
            InitializeSiteUC(SypCmdHandler);
        }

        private void ShowMain()
        {
                foreach (Control myControl in Controls)
                {
                    if (myControl is OptionMainPanel)
                    {
                        myControl.Show();
                    }
                    else if(myControl is siteControlPanel)
                        myControl.Hide();
                }
                
        }

        private void HideUCExcept(int SiteId)
        {
            foreach (Control myControl in Controls)
            {
                if (myControl is siteControlPanel || myControl is OptionMainPanel)
                {
                    if (myControl.Name != SiteId.ToString())
                        myControl.Hide();
                    else
                        myControl.Show();
                    //((SypItemPrefPanel)myControl).ProcessConfig();
                }
            }

        }

        public void SiteSelect(int SiteId)
        {
            foreach (MozItem myItem in MenuPane.Items)
            {
                if (myItem.Name == SiteId.ToString())
                    myItem.SelectItem();
            }
            HideUCExcept(SiteId);
        }

        public void SiteMenuItem_Click(object sender, EventArgs e)
        {
            if (sender != null)
            {
                int SiteId = -1;

                if (int.TryParse(((MozItem)sender).Name, out SiteId))
                {
                        HideUCExcept(SiteId);
                }
                else
                    OutputMethod("[OptionMenu-EventOnClick]Cannot cast in int, following string expression : " + ((MozItem)sender).Name, Output.ERROR);
            }
            PrefStatusBar("");
            PerformLayout();
        }

        private void SypPrefForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.timeToClose)
            {
                e.Cancel = false;
            }
            else
                e.Cancel = true;
            Hide();

            foreach (Control myControl in Controls)
            {
                if (myControl is SypUserControl)
                {
                    ((SypUserControl)myControl).ProcessConfig();
                }
            }
            OutputMethod("Serializeall-site", Output.OUT);
            OutputMethod("Serializeall-site", Output.COMMAND);
            SypGarbage.SaveSypConf();

            OutputMethod(string.Empty, Output.UNLOCK);
        }

       


        private void MenuItemGeneral_Click(object sender, EventArgs e)
        {
            PrefStatusBar("");
            ShowMain();
        }

        

       

    }
}